package com.anti.modular.bus.buscostbilldeposit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.basicinformation.param.BasicInformationParam;
import com.anti.modular.bus.buscostbilldeposit.entity.BusCostBillDeposit;
import com.anti.modular.bus.buscostbilldeposit.enums.BusCostBillDepositExceptionEnum;
import com.anti.modular.bus.buscostbilldeposit.param.BusCostBillDepositParam;
import com.anti.modular.bus.buscostbilldeposit.service.BusCostBillDepositService;
import com.anti.modular.sys.dict.entity.SysDictData;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 費用類別service接口实现类
 *
 * @author anti
 * @date 2021-04-08 09:51:49
 */
@Service
public class BusCostBillDepositServiceImpl implements BusCostBillDepositService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BusCostBillDeposit getById(BusCostBillDepositParam busCostBillDepositParam){
        String sql="select * from bus_cost_bill_deposit where id =? ";
        return dbUtils.get(sql,BusCostBillDeposit.class,busCostBillDepositParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,BusCostBillDepositParam busCostBillDepositParam) {
        StringBuilder sb=new StringBuilder("select * from bus_cost_bill_deposit where 1=1 and status=0 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busCostBillDepositParam)) {
            // 根据押金名称 查询
            if (ObjectUtil.isNotEmpty(busCostBillDepositParam.getDepositName())) {
               sb.append(" and deposit_name like ?");
               param.add("%"+busCostBillDepositParam.getDepositName()+"%");
            }
            // 根据计数 （字典 0不计数 1计数） 查询
            if (ObjectUtil.isNotEmpty(busCostBillDepositParam.getCount())) {
               sb.append(" and count = ?");
               param.add(busCostBillDepositParam.getCount());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BusCostBillDeposit> list(BusCostBillDepositParam busCostBillDepositParam) {
        String sql="select * from bus_cost_bill_deposit where  status=0";
        return dbUtils.query(sql,BusCostBillDeposit.class);
    }

    @Override
    public void add(BusCostBillDepositParam busCostBillDepositParam) {
        checkParam(busCostBillDepositParam,false);
        BusCostBillDeposit busCostBillDeposit = new BusCostBillDeposit();
        BeanUtil.copyProperties(busCostBillDepositParam, busCostBillDeposit);
        busCostBillDeposit.setStatus(0);
        busCostBillDeposit.setCreateTime(new Date());
        busCostBillDeposit.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(busCostBillDeposit);
    }


    public void delete(BusCostBillDepositParam busCostBillDepositParam) {
        BusCostBillDeposit busCostBillDeposit = this.queryBusCostBillDeposit(busCostBillDepositParam);
        busCostBillDeposit.setStatus(2);
        busCostBillDeposit.setUpdateTime(new Date());
        busCostBillDeposit.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busCostBillDeposit);
    }

    public void edit(BusCostBillDepositParam busCostBillDepositParam) {
        checkParam(busCostBillDepositParam,true);
        BusCostBillDeposit busCostBillDeposit = this.queryBusCostBillDeposit(busCostBillDepositParam);
        BeanUtil.copyProperties(busCostBillDepositParam, busCostBillDeposit);
        busCostBillDeposit.setUpdateTime(new Date());
        busCostBillDeposit.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busCostBillDeposit);
    }

    @Override
    public BusCostBillDeposit detail(BusCostBillDepositParam busCostBillDepositParam) {
        return this.queryBusCostBillDeposit(busCostBillDepositParam);
    }

    /**
     * 获取費用類別
     *
     * @author anti
     * @date 2021-04-08 09:51:49
     */
    private BusCostBillDeposit queryBusCostBillDeposit(BusCostBillDepositParam busCostBillDepositParam) {
        BusCostBillDeposit busCostBillDeposit = this.getById(busCostBillDepositParam);
        if (ObjectUtil.isNull(busCostBillDeposit)) {
            throw new BusinessException(BusCostBillDepositExceptionEnum.NOT_EXIST);
        }
        return busCostBillDeposit;
    }


    private void checkParam(BusCostBillDepositParam busCostBillDepositParam, boolean isExcludeSelf){
        Long id = busCostBillDepositParam.getId();
        String depositName = busCostBillDepositParam.getDepositName();
        //构建带name查询条件
        StringBuilder nameSb=new StringBuilder("select count(*) from bus_cost_bill_deposit where status =0 and deposit_name =?");
        ArrayList<Object> nameParams = CollectionUtil.newArrayList();
        nameParams.add(depositName);
        //如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            nameSb.append(" and id <> ?");
            nameParams.add(id);
        }

        //查询重复记录的数量
        int countByName = dbUtils.getIntValue(nameSb.toString(),nameParams.toArray());

        //如果存在重复的记录，抛出异常，直接返回前端
        if (countByName >= 1) {
            throw new BusinessException(0,"該押金已存在");
        }
    }
}
